package com.jfinal.bbsapp.sys;

import java.util.Set;
import org.apache.log4j.Logger;
import com.jfinal.aop.Clear;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;

@Path("/sys")
public class SysController extends Controller {
	private final Logger logger = Logger.getLogger(SysController.class);

	@Inject
	SysService srv;

	//发送验证码
	@Clear
	public void sendmsg() {
		renderJson(srv.sendmsg(getPara("mobile").trim(),getPara("ischeckm")));
	}
		
	//手机号注册
	@Clear
	public void reg() {
		renderJson(srv.reg(getPara("mobile").trim(), getPara("code").trim()));
	}
	
	//登录
	@Clear
	public void login() {
		renderJson(srv.login(getPara("mobile"), getPara("password"), getPara("code")));
	}
	
	//找回密码
	@Clear
	public void findpwd() {
		renderJson(srv.findpwd(getPara("mobile"), getPara("code"), getPara("password")));
	}
	
	//获取appid
	public void getappid() { 
		renderJson(srv.getappid(getRequest().getHeader("token"),getPara("realname"),getPara("sourcephotostr")));
	}

	//用户注册完第一次人脸核身成功结果更新
	public void fchecksuccess() {
		renderJson(srv.fchecksuccess(getRequest().getHeader("token")));
	}
	
	//用户第二次以及以后N次人脸核身结果更新
	public void facecheck() {
		renderJson(srv.facecheck(getRequest().getHeader("token"),getPara("flag")));
	}
	
	//根据token查询用户信息
	public void getuser(){
		renderJson(srv.getuser(getRequest().getHeader("token")));
	}
	
	//根据用户id查询用户信息
	public void getuserbyid(){
		renderJson(srv.getuserbyid(getRequest().getHeader("token"),getPara("userid")));
	}
	
	//我的推荐人
	public void mypre() {
		renderJson(srv.mypre(getRequest().getHeader("token")));
	}
		
	//更新头像
	public void upheadimg(){
		renderJson(srv.upheadimg(getRequest().getHeader("token"), getPara("headimg")));
	}
	
	//同时更新头像与昵称并绑定上级
	public void upheadnick(){
		renderJson(srv.upheadnick(getRequest().getHeader("token"), getPara("headimg"), getPara("nickname")));
	}
	
	//更新昵称
	public void upnickname(){
		renderJson(srv.upnickname(getRequest().getHeader("token"), getPara("nickname")));
	}
	
	//根据token更新用户手机号
	public void upmobile(){
		renderJson(srv.upmobile(getRequest().getHeader("token"), getPara("code")));
	}

	//注销
	@Clear
	public void logout() {
		try {
			String token = getHeader("token");
			Cache res = Redis.use();
			Set<String> keys = res.keys("app-" + token + "-*");
			if (keys.size() > 0) {
				for (String key : keys) {
					res.del(key);
				}
			}
		} catch (Exception e) {
			logger.error("------Exception------", e);
			renderJson(Ret.fail("注销失败").set("code", 50000));	
		}
		renderJson(Ret.ok("注销成功").set("code", 20000));	
	}
	
	//获取版本信息
	@Clear
	public void getversion(){
		renderJson(srv.getversion(getPara("clienttype"),getPara("systype")));
	}
	
	//修改登录密码
	public void uppwd(){
		renderJson(srv.updpwd(getPara("userid"),getPara("oldpwd"),getPara("pwd")));
	}
}
